package com.maple.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.maple.admin.common.util.AjaxJson;
import com.maple.admin.domain.entity.Apply;
import com.maple.admin.domain.entity.Detail;
import com.maple.admin.domain.entity.ProductOne;
import com.maple.admin.service.*;
import io.swagger.annotations.Api;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/product")
@Api(value = "product", tags = "商品模块")
public class ProductController {

    @Resource
    private ProductOneService productOneService;
    @Resource
    private ProductTwoService productTwoService;

    @Resource
    private ApplyService applyService;

    @Resource
    private DetailService detailService;

    @Resource
    private ShopService shopService;

    @GetMapping("/list")
    public AjaxJson selectAll(@RequestParam String pname){
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.like("pname",pname);
        return  AjaxJson.getSuccessData(productOneService.list(wrapper));
    }
    @GetMapping("/getDict")
    public AjaxJson getDict(){
        return AjaxJson.getSuccessData(productTwoService.list());
    }
    @PostMapping("/saveOrUpdate")
    public AjaxJson saveOrUpdate(@RequestBody ProductOne productOne){

        return AjaxJson.getSuccessData(productOneService.saveOrUpdate(productOne));
    }
    @GetMapping("/delete/{pid}")
    @Transactional
    public AjaxJson delete(@PathVariable Integer pid){
        List<Apply> appyList = applyService.getApplyByPid(pid);
        for (Apply apply : appyList){
            applyService.removeById(apply.getAid());
        }

        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("pid",pid);

        detailService.remove(wrapper);

        QueryWrapper wrapper1 = new QueryWrapper<>();
        wrapper1.eq("pid",pid);

        shopService.remove(wrapper1);

        return AjaxJson.getSuccessData(productOneService.removeById(pid));
    }
}
